package com.wjerp.tplus.server.application.biz;

import com.wjerp.tplus.server.api.request.AddInvoiceInfoRequest;
import com.wjerp.tplus.server.api.request.QueryInvoiceListRequest;
import com.wjerp.tplus.server.api.response.InvoiceInfoResponse;
import com.wjerp.tplus.server.api.response.PageDataResponse;
import com.wjerp.tplus.server.application.CommonService;
import com.wjerp.tplus.server.domain.entity.CompanyInfo;
import com.wjerp.tplus.server.domain.entity.CompanyInvoiceRelation;
import com.wjerp.tplus.server.domain.entity.invoice.mng.InvoiceInfo;
import com.wjerp.tplus.server.domain.entity.invoice.mng.ReimburseMemberInfo;
import com.wjerp.tplus.server.domain.entity.sys.UserInfo;
import com.wjerp.tplus.server.domain.service.*;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 发票应用聚合层
 * @author lishuailei
 */
@Component
public class InvoiceBiz implements CommonService {

    @Resource
    private InvoiceInfoService invoiceInfoService;

    @Resource
    private ReimburseMemberService reimburseMemberService;

    @Resource
    private CompanyInvoiceRelationService companyInvoiceRelationService;

    @Resource
    private CompanyService companyService;

    /**
     * 获取发票信息（分页）
     * @param request 查询入参
     * @return 发票信息分页结果
     */
    public PageDataResponse<InvoiceInfoResponse> loadInvoiceInfoList(QueryInvoiceListRequest request) {
        //1.根据条件获取发票基本信息列表
        List<InvoiceInfo> invoiceInfoList = invoiceInfoService.loadInvoiceInfoList(request);
        //获取总数
        Integer totalCount = 0;
        if(!CollectionUtils.isEmpty(invoiceInfoList)){
            totalCount = invoiceInfoService.loadInvoiceInfoListCount(request);
        }

        List<InvoiceInfoResponse> invoiceInfoResponseList = new ArrayList<>();
        if( totalCount > 0 ){
            //2.填充操作用户信息
            List<String> operatorUserIdList = invoiceInfoList.stream().map(InvoiceInfo::getCreatedUser).distinct().collect(Collectors.toList());
            operatorUserIdList.addAll(invoiceInfoList.stream().map(InvoiceInfo::getUpdatedUser).distinct().collect(Collectors.toList()));
            Map<String, List<UserInfo>> userInfoMap = loadUserInfoMap(operatorUserIdList);
            //3.填充报销人员信息
            List<String> reimburseMemberCodeList = invoiceInfoList.stream().map(InvoiceInfo::getReimburseMemberCode).distinct().collect(Collectors.toList());
            List<ReimburseMemberInfo> reimburseMemberInfoList = reimburseMemberService.loadReimburseMemberByReimburseMemberCodeList(reimburseMemberCodeList);
            Map<String, List<ReimburseMemberInfo>> reimburseMemberMap = reimburseMemberInfoList.stream().collect(Collectors.groupingBy(ReimburseMemberInfo::getMemberCode));

            //4.填充公司信息
            List<String> invoiceUuidCodeList = invoiceInfoList.stream().map(InvoiceInfo::getCode).distinct().collect(Collectors.toList());
            List<CompanyInvoiceRelation> companyInvoiceRelations = companyInvoiceRelationService.selectByInvoiceUuidCodeList(invoiceUuidCodeList);
            Map<String, List<CompanyInvoiceRelation>> companyInvoiceMap = companyInvoiceRelations.stream().collect(Collectors.groupingBy(CompanyInvoiceRelation::getInvoiceUuidCode));

            List<String> companyCodeList = companyInvoiceRelations.stream().map(CompanyInvoiceRelation::getCompanyCode).distinct().collect(Collectors.toList());
            List<CompanyInfo> companyInfos = companyService.loadCompanyInfoByCompanyCodeList(companyCodeList);
            Map<String, List<CompanyInfo>> companyMap = companyInfos.stream().collect(Collectors.groupingBy(CompanyInfo::getCompanyCode));

            invoiceInfoList.stream().forEach(data->{
                InvoiceInfoResponse invoiceInfoResponse = new InvoiceInfoResponse();
                BeanUtils.copyProperties(data,invoiceInfoResponse);
                invoiceInfoResponse.setCreatedUsername(userInfoMap.get(data.getCreatedUser()).get(0).getName());
                invoiceInfoResponse.setUpdatedUsername(userInfoMap.get(data.getUpdatedUser()).get(0).getName());
                invoiceInfoResponse.setReimburseMemberName(reimburseMemberMap.get(data.getReimburseMemberCode()).get(0).getName());
                invoiceInfoResponse.setCompanyName(companyMap.get(companyInvoiceMap.get(data.getCode()).get(0).getCompanyCode()).get(0).getName());
                invoiceInfoResponseList.add(invoiceInfoResponse);
            });
        }
        return  new PageDataResponse<>(totalCount,invoiceInfoResponseList);
    }
}
